﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using V5_DBUtility;
using V5_Model;
using System.Data.SqlClient;

namespace V5_DAL.SqlServer
{
    public class DALGZCustomOutPrice
    {
        #region  Method


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid CustomID, Guid ProductID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Bc_CustomOutPrice");
            strSql.Append(" where CustomID='" + CustomID + "' and ProductID='" + ProductID + "' ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelGZCustomOutPrice model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ID != null)
            {
                strSql1.Append("ID,");
                strSql2.Append("'" + Guid.NewGuid().ToString() + "',");
            }
            if (model.CustomID != null)
            {
                strSql1.Append("CustomID,");
                strSql2.Append("'" + Guid.NewGuid().ToString() + "',");
            }
            if (model.ProductID != null)
            {
                strSql1.Append("ProductID,");
                strSql2.Append("'" + Guid.NewGuid().ToString() + "',");
            }
            if (model.OutBulkPrice != null)
            {
                strSql1.Append("OutBulkPrice,");
                strSql2.Append("" + model.OutBulkPrice + ",");
            }
            if (model.OutPiecePrice != null)
            {
                strSql1.Append("OutPiecePrice,");
                strSql2.Append("" + model.OutPiecePrice + ",");
            }
            if (model.LastOutBulkPrice != null)
            {
                strSql1.Append("LastOutBulkPrice,");
                strSql2.Append("" + model.LastOutBulkPrice + ",");
            }
            if (model.LastOutPicePrice != null)
            {
                strSql1.Append("LastOutPicePrice,");
                strSql2.Append("" + model.LastOutPicePrice + ",");
            }
            if (model.PromotionsStartDate != null)
            {
                strSql1.Append("PromotionsStartDate,");
                strSql2.Append("'" + model.PromotionsStartDate + "',");
            }
            if (model.PromotionsEndDate != null)
            {
                strSql1.Append("PromotionsEndDate,");
                strSql2.Append("'" + model.PromotionsEndDate + "',");
            }
            if (model.PromotionsPrice != null)
            {
                strSql1.Append("PromotionsPrice,");
                strSql2.Append("" + model.PromotionsPrice + ",");
            }
            if (model.TimeStamp != null)
            {
                strSql1.Append("TimeStamp,");
                strSql2.Append("" + model.TimeStamp + ",");
            }
            strSql.Append("insert into Bc_CustomOutPrice(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelGZCustomOutPrice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Bc_CustomOutPrice set ");
            if (model.ID != null)
            {
                strSql.Append("ID='" + model.ID + "',");
            }
            if (model.OutBulkPrice != null)
            {
                strSql.Append("OutBulkPrice=" + model.OutBulkPrice + ",");
            }
            else
            {
                strSql.Append("OutBulkPrice= null ,");
            }
            if (model.OutPiecePrice != null)
            {
                strSql.Append("OutPiecePrice=" + model.OutPiecePrice + ",");
            }
            else
            {
                strSql.Append("OutPiecePrice= null ,");
            }
            if (model.LastOutBulkPrice != null)
            {
                strSql.Append("LastOutBulkPrice=" + model.LastOutBulkPrice + ",");
            }
            else
            {
                strSql.Append("LastOutBulkPrice= null ,");
            }
            if (model.LastOutPicePrice != null)
            {
                strSql.Append("LastOutPicePrice=" + model.LastOutPicePrice + ",");
            }
            else
            {
                strSql.Append("LastOutPicePrice= null ,");
            }
            if (model.PromotionsStartDate != null)
            {
                strSql.Append("PromotionsStartDate='" + model.PromotionsStartDate + "',");
            }
            else
            {
                strSql.Append("PromotionsStartDate= null ,");
            }
            if (model.PromotionsEndDate != null)
            {
                strSql.Append("PromotionsEndDate='" + model.PromotionsEndDate + "',");
            }
            else
            {
                strSql.Append("PromotionsEndDate= null ,");
            }
            if (model.PromotionsPrice != null)
            {
                strSql.Append("PromotionsPrice=" + model.PromotionsPrice + ",");
            }
            else
            {
                strSql.Append("PromotionsPrice= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where CustomID='" + model.CustomID + "' and ProductID='" + model.ProductID + "' ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid CustomID, Guid ProductID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Bc_CustomOutPrice ");
            strSql.Append(" where CustomID='" + CustomID + "' and ProductID='" + ProductID + "' ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelGZCustomOutPrice GetModel(Guid CustomID, Guid ProductID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" ID,CustomID,ProductID,OutBulkPrice,OutPiecePrice,LastOutBulkPrice,LastOutPicePrice,PromotionsStartDate,PromotionsEndDate,PromotionsPrice,TimeStamp ");
            strSql.Append(" from Bc_CustomOutPrice ");
            strSql.Append(" where CustomID='" + CustomID + "' and ProductID='" + ProductID + "' ");
            ModelGZCustomOutPrice model = new ModelGZCustomOutPrice();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = new Guid(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CustomID"] != null && ds.Tables[0].Rows[0]["CustomID"].ToString() != "")
                {
                    model.CustomID = new Guid(ds.Tables[0].Rows[0]["CustomID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProductID"] != null && ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                {
                    model.ProductID = new Guid(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OutBulkPrice"] != null && ds.Tables[0].Rows[0]["OutBulkPrice"].ToString() != "")
                {
                    model.OutBulkPrice = decimal.Parse(ds.Tables[0].Rows[0]["OutBulkPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OutPiecePrice"] != null && ds.Tables[0].Rows[0]["OutPiecePrice"].ToString() != "")
                {
                    model.OutPiecePrice = decimal.Parse(ds.Tables[0].Rows[0]["OutPiecePrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastOutBulkPrice"] != null && ds.Tables[0].Rows[0]["LastOutBulkPrice"].ToString() != "")
                {
                    model.LastOutBulkPrice = decimal.Parse(ds.Tables[0].Rows[0]["LastOutBulkPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastOutPicePrice"] != null && ds.Tables[0].Rows[0]["LastOutPicePrice"].ToString() != "")
                {
                    model.LastOutPicePrice = decimal.Parse(ds.Tables[0].Rows[0]["LastOutPicePrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PromotionsStartDate"] != null && ds.Tables[0].Rows[0]["PromotionsStartDate"].ToString() != "")
                {
                    model.PromotionsStartDate = DateTime.Parse(ds.Tables[0].Rows[0]["PromotionsStartDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PromotionsEndDate"] != null && ds.Tables[0].Rows[0]["PromotionsEndDate"].ToString() != "")
                {
                    model.PromotionsEndDate = DateTime.Parse(ds.Tables[0].Rows[0]["PromotionsEndDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PromotionsPrice"] != null && ds.Tables[0].Rows[0]["PromotionsPrice"].ToString() != "")
                {
                    model.PromotionsPrice = decimal.Parse(ds.Tables[0].Rows[0]["PromotionsPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TimeStamp"] != null && ds.Tables[0].Rows[0]["TimeStamp"].ToString() != "")
                {
                    model.TimeStamp = DateTime.Parse(ds.Tables[0].Rows[0]["TimeStamp"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,CustomID,ProductID,OutBulkPrice,OutPiecePrice,LastOutBulkPrice,LastOutPicePrice,PromotionsStartDate,PromotionsEndDate,PromotionsPrice,TimeStamp ");
            strSql.Append(" FROM Bc_CustomOutPrice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,CustomID,ProductID,OutBulkPrice,OutPiecePrice,LastOutBulkPrice,LastOutPicePrice,PromotionsStartDate,PromotionsEndDate,PromotionsPrice,TimeStamp ");
            strSql.Append(" FROM Bc_CustomOutPrice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  Method

    }
}